<?php
/*****************************************
 * This file is part of SDM Dynamo CMS   *
 * Copyright (c) Sean Micklethwaite 2006 *
 * All rights reserved                   *
 ****************************************/
 
class cDycmsVfsQuery extends cDycmsQuery
{
	protected $msDisplay;
	protected $mBaseCriteria;
	protected $mResult;
	protected $mParentId;
	
	public $mPath;
	public $mName;
	
	function __construct($path = NULL)
	{
		$this->mBaseCriteria = array();
		
		$path = dyFormatPath($path);
		$this->mPath = $path;
		if($path)
		{
			//
			// Parse the path
			//
			
			$pz = split("/", $path);
			$sval = $pz[count($pz) - 1];
			
			$np = implode("/", array_splice($pz,0, count($pz)-1));
			$pq = new cDycmsVfsQuery($np);
			$bc = $pq->GetBaseCriteria();
			
			//
			// Find display value
			//
			
			$res = $pq->GetResult();
			$disp = NULL;
			
			foreach($res as $cdisp => $valz)
			{
				foreach($valz as $val)
				{
					if($val == $sval)
					{
						$disp = $cdisp;
						break;
					};
				};
				if($disp)
					break;
			}
			array_push($bc, "$disp:$val");
			$this->mBaseCriteria = $bc;
			
			//
			// Get ID of parent
			//
			
			$q = new cDycmsQuery("obj-parent:".$pq->GetParentId(),
				"vfs-display:$disp");
			$q->SetClass('c6979000134e11dbac5d0800200c9a66');
			$n = $q->Execute();
			
			if($n > 1)
				dyDie("Ambiguous query in fetching VFS object");
			elseif($n == 0)
			{
				$this->mBaseCriteria = NULL;
				return;
			};
			
			$this->mParentId = $q->mObject->GetId();
		}
		else
		{
			$this->mParentId = 0;
		};
		
		//
		// Add VFS Objects
		//
		
		$q = new cDycmsQuery("obj-parent:".$this->mParentId);
		$q->SetClass('c6979000134e11dbac5d0800200c9a66');
		$n = $q->Execute();
		
		for($c = 0; $c < $n; $c++)
		{
			$disp = $q->mObject->Get('vfs-display', $c);
			$this->mName[$disp] = $q->mObject->Get('name', $c);
			$this->mResult[$disp] = array();
			
			$this->Flush();
			$this->AddArray(array("$disp:*"));
			$this->AddArray($this->mBaseCriteria);
			$this->Execute();
			
			//
			// Get unique attribute values
			//
			
			$this->mResult[$disp] = $this->mObject->GetUniqueValues($disp);
		};
	}
	
	function Display()
	{
		global $p;
		if(count($this->mResult))
		{
			foreach($this->mResult as $name => $valz)
			{
				echo( "<table><tr><th>");
				uiIcon("collection", false);
				echo "</th><th>".$this->mName[$name]."</th></tr>\n";
				foreach($valz as $val)
				{
					echo( "<tr><td>");
					uiIcon("folder", false);
					echo("</td><td><a href=\"".dyBrowserUrl($val)."\">"
							. $val . "</a></td></tr>\n");
				};
				echo "</table>\n";
			};
		}
		else
		{
			if($this->mBaseCriteria)
			{
				if(!$this->mObject)
				{
					$this->Flush();
					$this->AddArray($this->mBaseCriteria);
					$this->Execute();
				};
				echo $this->mObject->GetDisplay();
			}
			else
				uiErrorCode(ENOTFOUND);
		};
	}
	
	function GetSummary()
	{
		if(count($this->mResult))
		{
			if($this->mPath)
				return $this->mPath;
			else
				return "Virtual Folder Root";
		}
		else
		{
			if($this->mBaseCriteria)
			{
				if(!$this->mObject)
				{
					$this->Flush();
					$this->AddArray($this->mBaseCriteria);
					$this->Execute();
				};
				return $this->mObject->GetSummary();
			}
			else
				return "Error";
		}
	}
	
	function GetResult()
	{
		return $this->mResult;
	}
	
	function GetBaseCriteria()
	{
		return $this->mBaseCriteria;
	}
	
	function GetParentId()
	{
		return $this->mParentId;
	}
}

function dyFormatPath($path)
{
	//TODO
	return $path;
}

?>